library(fixest)
library(kableExtra)
library(magrittr)
library(tidyverse)
library(texreg)
library(ggiplot)


################################################################################
# Data Preparation
################################################################################

# read data
df <- read_csv("../data/regdata.csv") %>% filter(Land == 7)

# green votes in 2009
green_2009 <- df %>%
    filter(year == 2009) %>%
    select(Verbandsgemeindeschluessel, Kreisschluessel, GRUENE_share) %>%
    group_by(Kreisschluessel) %>%
    mutate(GRUENE_share_2009_median = median(GRUENE_share)) %>%
    ungroup() %>%
    mutate(GRUENE_share_2009_hi = GRUENE_share > GRUENE_share_2009_median) %>%
    select(Verbandsgemeindeschluessel, GRUENE_share_2009_hi)
df <- left_join(df, green_2009, by = "Verbandsgemeindeschluessel")


################################################################################
# Regression with and without Green stronghold information
################################################################################
rr1 <- feols(
    GRUENE_share ~ flooded_area_p |Verbandsgemeindeschluessel + year^Kreisschluessel,
    cluster = ~Verbandsgemeindeschluessel,
    data = df,
    weights = ~Gueltige
)
rr2 <- feols(
    GRUENE_share ~ flooded_area_p*GRUENE_share_2009_hi |Verbandsgemeindeschluessel + year^Kreisschluessel,
    cluster = ~Verbandsgemeindeschluessel,
    data = df,
    weights = ~Gueltige
)
rr3 <- feols(
    GRUENE_share ~ ratio_total_p |Verbandsgemeindeschluessel + year^Kreisschluessel,
    cluster = ~Verbandsgemeindeschluessel,
    data = df,
    weights = ~Gueltige
)
rr4 <- feols(
    GRUENE_share ~ ratio_total_p*GRUENE_share_2009_hi |Verbandsgemeindeschluessel + year^Kreisschluessel,
    cluster = ~Verbandsgemeindeschluessel,
    data = df,
    weights = ~Gueltige
)
rr5 <- feols(
    GRUENE_share ~ ratio_damaged_p |Verbandsgemeindeschluessel + year^Kreisschluessel,
    cluster = ~Verbandsgemeindeschluessel,
    data = df,
    weights = ~Gueltige
)
rr6 <- feols(
    GRUENE_share ~ ratio_damaged_p*GRUENE_share_2009_hi |Verbandsgemeindeschluessel + year^Kreisschluessel,
    cluster = ~Verbandsgemeindeschluessel,
    data = df,
    weights = ~Gueltige
)

setFixest_dict(
    c(
        GRUENE_share = "Percentage of votes for Green Party",
        log_to_flooded_p = "Log distance to any flooding",
        Verbandsgemeindeschluessel = "Municipality",
        Kreisschluessel = "County",
        year = "Year",
        to_flooded_0_10km_p = "0 to 10km from any flooding",
        to_flooded_10_20km_p = "10 to 20km from any flooding",
        log_to_flooded_p = "Log distance to any flooding",
        flooded_area_p = "Share of area flooded",
        ratio_damaged_p = "Share of buildings damaged",
        ratio_total_p = "Share of buildings affected",
        GRUENE_share_2009_hiTRUE = "Green stronghold"
    )
)
setFixest_etable(
    fitstat = c("my", "n", "ar2", "wr2"),
    style.tex = style.tex("aer"),
    digits.stats = 4
)

results <- list(rr1, rr2, rr3, rr4, rr5, rr6)
esttex(
    results,
    file = "../results/tables/green_stronghold.tex",
    replace = TRUE,
    drop = "Constant"
)
